package com.kedacom.demo.sigar;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.hyperic.sigar.Cpu;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.DirStat;
import org.hyperic.sigar.DirUsage;
import org.hyperic.sigar.FileInfo;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemMap;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.MultiProcCpu;
import org.hyperic.sigar.NetConnection;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.NetRoute;
import org.hyperic.sigar.NetStat;
import org.hyperic.sigar.ProcMem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;


public class SigarDemo {


	
	public static void main(String[] args) throws SigarException, UnknownHostException {
		Sigar sigar = new Sigar();
		
		System.out.println("VERSION_STRING:"+Sigar.VERSION_STRING);
		System.out.println("BUILD_DATE:"+Sigar.BUILD_DATE);
		System.out.println("NATIVE_BUILD_DATE:"+Sigar.NATIVE_BUILD_DATE);
		System.out.println("NATIVE_VERSION_STRING:"+Sigar.NATIVE_VERSION_STRING);
		System.out.println("SCM_REVISION:"+Sigar.SCM_REVISION);

		
		CpuInfo[] cpuInfos = sigar.getCpuInfoList();
		for(int i=0;i<cpuInfos.length;i++){
			CpuInfo cpuInfo = cpuInfos[i];
			System.out.println("CpuInfo["+i+"]:"+cpuInfo.toString());
		}
		
		Cpu[] cpus = sigar.getCpuList();
		for(int i=0;i<cpus.length;i++){
			Cpu cpu = cpus[i];
			System.out.println("Cpu["+i+"]:"+cpu.toString());
		}
		
		
		CpuPerc cpuPercTotal =sigar.getCpuPerc();
		System.out.println("CpuPercTotal:"+cpuPercTotal.toString());
		
		CpuPerc[] cpuPercs = sigar.getCpuPercList();
		for(int i=0;i<cpuPercs.length;i++){
			CpuPerc cpuPerc = cpuPercs[i];
			System.out.println("CpuPerc["+i+"]:"+cpuPerc.toString());
		}
		
		DirStat dirStat=sigar.getDirStat("F:\\");
		System.out.println("DirStat-F:\\:"+dirStat.toString());
		
		DirUsage dirUsage=sigar.getDirUsage("F:\\");
		System.out.println("DirUsage-F:\\:"+dirUsage.toString());
	
		FileInfo fileInfo=sigar.getFileInfo("F:\\");
		System.out.println("FileInfo-F:\\:"+fileInfo.toString());
		
		FileSystem[] fileSystems = sigar.getFileSystemList();
		for(int i=0;i<fileSystems.length;i++){
			FileSystem fileSystem = fileSystems[i];
			System.out.println("FileSystem["+i+"]:"+fileSystem.toString());
			System.out.println("DevName["+i+"]:"+fileSystem.getDevName());
			System.out.println("getDirName["+i+"]:"+fileSystem.getDirName());			
			System.out.println("getFlags["+i+"]:"+fileSystem.getFlags());			
			System.out.println("getOptions["+i+"]:"+fileSystem.getOptions());			
			System.out.println("getSysTypeName["+i+"]:"+fileSystem.getSysTypeName());			
			System.out.println("getType["+i+"]:"+fileSystem.getType());			
			System.out.println("getTypeName["+i+"]:"+fileSystem.getTypeName());
		}
		
		
		FileSystemMap fileSystemMap=sigar.getFileSystemMap();
		System.out.println("FileSystemMap:"+fileSystemMap.toString());
		
		FileSystemUsage fileSystemUsage = sigar.getFileSystemUsage("F:\\");
		System.out.println("FileSystemUsage-F:\\:"+fileSystemUsage.toString());
		
		System.out.println("getFQDN:"+sigar.getFQDN());
		
		FileInfo  linkInfo = sigar.getLinkInfo("F:\\");
		System.out.println("LinkInfo-F:\\:"+linkInfo.toString());
		
		//not suppor for windows7
		/*try{
			double[] doubles = sigar.getLoadAverage();
			for(int i=0;i<doubles.length;i++){
				System.out.println("getLoadAverage["+i+"]:"+doubles[i]);
	
			}
		}catch(Exception e){
			e.printStackTrace();
		}*/
		
		Mem mem =sigar.getMem();
		System.out.println("Mem:"+mem.toString());
		System.out.println("getActualFree:"+mem.getActualFree()/1024/1024);
		System.out.println("getActualUsed:"+mem.getActualUsed()/1024/1024);
		System.out.println("getFree:"+mem.getFree()/1024/1024);
		System.out.println("getFreePercent:"+mem.getFreePercent());
		System.out.println("getRam:"+mem.getRam());
		System.out.println("getTotal:"+mem.getTotal()/1024/1024);
		System.out.println("getUsed:"+mem.getUsed()/1024/1024);
		System.out.println("getUsedPercent:"+mem.getUsedPercent());
		
		FileSystemUsage  mountFileSystemUsage= sigar.getMountedFileSystemUsage("F:\\");
		System.out.println("getMountedFileSystemUsage-F:\\:"+mountFileSystemUsage.toString());
		
		//PTQL查询字符串用法:https://support.hyperic.com/display/SIGAR/PTQL
		MultiProcCpu  multiProcCpu= sigar.getMultiProcCpu("State.Name.re=mysqld");
		System.out.println("getMultiProcCpu:"+multiProcCpu.toString());
		
		ProcMem   procMem = sigar.getMultiProcMem("State.Name.re=mysqld");
		System.out.println("getMultiProcMem:"+procMem.toString());
		

		 NetConnection[]  netConnections =sigar.getNetConnectionList(NetFlags.CONN_CLIENT);
		 for(int i=0;i<netConnections.length;i++){
			 NetConnection netConnection = netConnections[i];
			 System.out.println("NetConnection["+i+"]:"+netConnection.toString());
		}
		
		System.out.println("getNetInfo:"+sigar.getNetInfo());
		System.out.println("getNetInterfaceConfig:"+sigar.getNetInterfaceConfig());

		
		NetRoute[] netRoutes = sigar.getNetRouteList();
		for(int i=0;i<netRoutes.length;i++){
			NetRoute netRoute = netRoutes[i];
			System.out.println("getNetRouteList:"+netRoute.toString());
		}
		
		
		System.out.println("getNetStat:"+sigar.getNetStat().getAllInboundTotal());
		System.out.println("getPid:"+sigar.getPid());
		//not suppor for windows7
		/*try{
			System.out.println("getNfsClientV2:"+sigar.getNfsClientV2());
			System.out.println("getNfsClientV3:"+sigar.getNfsClientV3());
			System.out.println("getNfsServerV2:"+sigar.getNfsServerV2());
			System.out.println("getNfsServerV3:"+sigar.getNfsServerV3());
		}catch(Exception e){
			e.printStackTrace();
		}*/
		
		System.out.println("getResourceLimit:"+sigar.getResourceLimit());
		System.out.println("getSwap:"+sigar.getSwap());
		System.out.println("getThreadCpu:"+sigar.getThreadCpu());
		System.out.println("getUptime:"+sigar.getUptime());
		System.out.println("getWhoList:"+sigar.getWhoList());
		System.out.println("getProcStat:"+sigar.getProcStat());
		System.out.println("getProcList:"+sigar.getProcList());
		
		String[] interfaces = sigar.getNetInterfaceList();
		for(int i=0;i<interfaces.length;i++){
			String iface = interfaces[i];
			NetInterfaceConfig netInterfaceConfig = sigar.getNetInterfaceConfig(iface);
			System.out.println("getNetInterfaceConfig["+iface+"]:"+netInterfaceConfig.toString());
			
			NetInterfaceStat netInterfaceStat =sigar.getNetInterfaceStat(iface);
			System.out.println("netInterfaceStat["+iface+"]:"+netInterfaceStat.toString());
			
		}
		
		System.out.println("getNetListenAddress[3306]:"+sigar.getNetListenAddress("3306"));
		System.out.println("getNetServicesName[tcp,3306]:"+sigar.getNetServicesName(NetFlags.CONN_TCP, 3306));
		//not suppor for windows7
		/*try{
			System.out.println("getProcPort[tcp,3306]:"+sigar.getProcPort(NetFlags.CONN_TCP, 3306));
		}catch(Exception e){
			e.printStackTrace();
		}*/
		
		
		InetAddress inetAddress = InetAddress.getLocalHost();
		NetStat netStat =sigar.getNetStat(inetAddress.getAddress(), 3306);
		System.out.println("getNetStat[getAllInboundTotal]:"+netStat.getAllInboundTotal());
		System.out.println("getNetStat[getAllOutboundTotal]:"+netStat.getAllOutboundTotal());
		System.out.println("getNetStat[getAllInboundTotal]:"+netStat.getTcpBound());
		System.out.println("getNetStat[getTcpClose]:"+netStat.getTcpClose());
		System.out.println("getNetStat[getTcpCloseWait]:"+netStat.getTcpCloseWait());
		System.out.println("getNetStat[getTcpClosing]:"+netStat.getTcpClosing());
		System.out.println("getNetStat[getTcpEstablished]:"+netStat.getTcpEstablished());
		System.out.println("getNetStat[getTcpFinWait1]:"+netStat.getTcpFinWait1());
		System.out.println("getNetStat[getTcpFinWait2]:"+netStat.getTcpFinWait2());
		System.out.println("getNetStat[getTcpIdle]:"+netStat.getTcpIdle());
		System.out.println("getNetStat[getTcpInboundTotal]:"+netStat.getTcpInboundTotal());
		System.out.println("getNetStat[getTcpLastAck]:"+netStat.getTcpLastAck());
		System.out.println("getNetStat[getTcpListen]:"+netStat.getTcpListen());
		System.out.println("getNetStat[getTcpOutboundTotal]:"+netStat.getTcpOutboundTotal());
		System.out.println("getNetStat[getTcpSynRecv]:"+netStat.getTcpSynRecv());
		System.out.println("getNetStat[getTcpSynSent]:"+netStat.getTcpSynSent());
		System.out.println("getNetStat[getTcpTimeWait]:"+netStat.getTcpTimeWait());
		int[] states=netStat.getTcpStates();
		for(int i=0;i<states.length;i++){
			//TCP连接状态详解:http://blog.csdn.net/mei922/article/details/4801858
			System.out.println("getNetStat[getTcpStates["+i+"]]:"+states[i]);
		}
		

		System.out.println("getServicePid[MySQL]:"+sigar.getServicePid("MySQL"));
		
		long pid = sigar.getServicePid("MySQL");
		String[] pargs = sigar.getProcArgs(pid);
		for(int i=0;i<pargs.length;i++){
			System.out.println("getProcArgs["+i+"]:"+pargs[i]);
		}
		
		System.out.println("getProcCpu[MySQL]:"+sigar.getProcCpu(pid));
		//not suppor for windows7
		/*try{
			System.out.println("getProcCred[MySQL]:"+sigar.getProcCred(pid));
		}catch(Exception e){
			e.printStackTrace();
		}*/
		System.out.println("getProcCredName[MySQL]:"+sigar.getProcCredName(pid));
		System.out.println("getProcEnv[MySQL]:"+sigar.getProcEnv(pid));
		System.out.println("getProcFd[MySQL]:"+sigar.getProcFd(pid));
		System.out.println("getProcMem[MySQL]:"+sigar.getProcMem(pid));
		System.out.println("getProcModules[MySQL]:"+sigar.getProcModules(pid));
		System.out.println("getProcState[MySQL]:"+sigar.getProcState(pid));
		System.out.println("getProcTime[MySQL]:"+sigar.getProcTime(pid));
		
		
		//测试Sigar工作原理
		/*while (true) {// 驻留主线程
			try {
				Thread.sleep(1000);
				cpuPercTotal = sigar.getCpuPerc();
				System.out.println("使用率:"+cpuPercTotal);
				long sigarPid = sigar.getPid();
				
				System.out.println("SigarPid:"+sigarPid);
				//System.out.println("SigarPid-getProcTime:"+sigar.getProcTime(sigarPid));
				//System.out.println("SigarPid-getProcMem:"+sigar.getProcMem(sigarPid));
				

			} catch (InterruptedException e) {
				// TODO 尚未处理异常
				e.printStackTrace();
			}
		}*/
		
		

	}

}
